package com.uvnos4j.matilda.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uvnos4j.matilda.commons.annotation.AppLog;
import com.uvnos4j.matilda.commons.dto.GridTable;
import com.uvnos4j.matilda.commons.dto.PageParam;
import com.uvnos4j.matilda.commons.dto.QueryParam;
import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.system.domain.SysDict;
import com.uvnos4j.matilda.system.service.SysDictService;
import com.uvnos4j.matilda.system.vo.DictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典 Controller
 * <p>
 * Description:字典 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.system.controller
 * @since 2020-08-31 23:16:04
 */
@Controller
@RequestMapping("/sys/dict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @AppLog(title = "字典管理")
    @PreAuthorize("hasAuthority('sys:dict')")
    @GetMapping("/list")
    public String list() {
        return "sys/dictlist";
    }

    @AppLog(title = "添加字典")
    @PreAuthorize("hasAuthority('sys:dict:add')")
    @ResponseBody
    @PostMapping
    public Result add(@Valid DictVo dictVo) {
        SysDict sysDict = dictVo.buildModel(SysDict.class, false);
        boolean result = sysDictService.save(sysDict);
        if (result) {
            return Result.success("数据字典添加成功", sysDict.getId().toString());
        }
        return Result.fail("数据字典添加失败");
    }

    @AppLog(title = "修改字典")
    @PreAuthorize("hasAuthority('sys:dict:update')")
    @ResponseBody
    @PutMapping
    public Result update(@Valid DictVo dictVo) {
        SysDict sysDict = dictVo.buildModel(SysDict.class, true);
        boolean result = sysDictService.updateById(sysDict);
        if (result) {
            return Result.success("数据字典修改成功", sysDict.getId().toString());
        }
        return Result.fail("数据字典修改失败");
    }

    @AppLog(title = "删除字典")
    @PreAuthorize("hasAuthority('sys:dict:del')")
    @ResponseBody
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> idList) {
        boolean result = sysDictService.removeByIds(idList);
        if (result) {
            return Result.success("数据字典删除成功,删除[" + idList.size() + "]条记录");
        }
        return Result.fail("数据字典删除失败");
    }

    @PreAuthorize("hasAuthority('sys:dict')")
    @ResponseBody
    @GetMapping
    public Result findPage() {
        // 组装分页信息
        PageParam pageParam = RequestUtil.packagePageParamByNumAndSize("page", "rows");
        // 组装排序信息
        String orderBy = RequestUtil.packageOrderBy("sort", "order", true);
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam param = RequestUtil.getQueryParam();

        IPage<SysDict> page = sysDictService.findPage(param, pageParam, orderBy);
        GridTable<SysDict> table = new GridTable<>(page.getTotal(), page.getRecords());

        return Result.success(table);
    }

    @PreAuthorize("hasAuthority('sys:dict')")
    @AppLog(title = "导出字典")
    @ResponseBody
    @PostMapping("/export")
    public Result export() {
        // 构建分页信息
        PageParam pageParam = PageParam.buildByNumAndSize(1, Integer.MAX_VALUE);
        // 组装排序信息
        String orderBy = RequestUtil.packageOrderBy("sort", "order", true);
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam param = RequestUtil.getQueryParam();

        String filename = param.getStringValue("filename");
        String fileDest = RequestUtil.getDownloadPath() + filename;

        sysDictService.export(param, pageParam, orderBy, fileDest);

        return Result.success("文件正在下载，请稍后……", fileDest);
    }

    /**
     * 根据类型查询数据字典
     * PreAuthorize 说明：登录用户可以进行调用
     *
     * @param types 类型集合
     * @return 字典列表
     */
    @ResponseBody
    @GetMapping("/findListByTypes")
    public Result findListByTypes(@RequestParam("types") String[] types) {
        List<SysDict> dictList = sysDictService.findListByTypes(types);
        return Result.success(dictList);
    }

}